<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    //用户的添加管理
    // 用户添加模块 
    public function getAdd(){
    	// echo '222';
    	return view('Admin.user.add');
    }
    // 执行用户插入
    public function postInsert(Request $request){
        // 查看提交的数据
        $this->validate($request, [
        'password' => 'required',
        'repassword' => 'required|same:password',
        // 'email' => 'required|email',
        'phone' => 'required|regex:[1\d{10}]',
        ],[
            'password.required'=>'用户密码不能为空',
            'repassword.required'=>'确认密码不能为空',
            'repassword.same'=>'两次密码不一致',
            'phone.required'=>'手机号不能为空', 
            'phone.regex'=>'请填写正确的手机号', 
        ]); 

        // 上面验证都成功
        // 提取部分数据
        $data = $request->only(['username','password','email','phone','sex','qx']);
        $data['token'] = str_random(50);
        $data['password'] = Hash::make($data['password']);
        $data['status'] = 2;
        // dd($request->all('data'));
        // 执行数据插入操作
        $res = DB::table('user')->insert($data);

        // var_dump($res);die;
        // // 判断是否添加成功
        if($res){
            return redirect('/admin/user')->with('success','用户添加成功');
        }else{
            return back()->with('error','用户添加失败');  
        }
    }
   


     public function getAjax(Request $request){
        $res = DB::table('user')->where('username',$request->input('name'))->first();
        //判断
        if($res){
            echo 0;
        }else{
            echo 1;
        }
    }
    
    public function getAjaxa(Request $request){
        $res = DB::table('user')->where('email',$request->input('name'))->first();
        //判断
        if($res){
            echo 0;
        }else{
            echo 1;
        }
    }





    public function getIndex(Request $request)
    {
        // echo '用户列表页';
        // 查询所有的用户数据
        // $users = DB::table('user')->get();
         // $users=DB::table('user');
         // var_dump($users);die;
        $num = $request->input('num',5);
        // var_dump($num);die;
        if($request->input('keywords')){

         $users = DB::table('user')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $users=DB::table('user')->paginate($num);
         
        }
        // dd($users);
        // 提取参数
        $data=$request->all();
        // var_dump($data);die;
        return view('Admin.user.index',['users'=>$users,'request'=>$data]);
    }


        public function getEdit($id) 
        {
            $user = DB::table('user')->where('id',$id)->first();
            // dd($user);
            // echo '2222';
            return view('admin.user.edit',['user'=>$user]);
        }









        // 用户数据修改

        public function postUpdate(Request $request)
        {
            // dd($request->all());
            // 获取id
            $id = $request->only(['id']);

            // 详细数据
            $data = $request->only(['username','email','phone','qx']);

            // dd($data);
           
            if($request->hasFile('pic')){
            //随机文件名
            	// dd(222);
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('pic')->getClientOriginalExtension();
            // dd($su);jpg png jpeg gif tiff
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href=url("/admin");
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('pic')->move('./uploads/', $name.'.'.$su);
            // echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            $data['pic']= '/uploads/'.$name.'.'.$su;
        }

        	// dd($data);
            // 执行修改
            $res = DB::table('user')->where('id',$id)->update($data);

            // 判断是否添加成功
             if($res){
                return redirect('/admin/user/index')->with('success','用户修改成功');
            }else{
                return back()->with('error','用户修改失败');  
            }
           
        }
         public function postDelete(Request $request)
            {
                // 获取id
                $id = $request->input('id');
                
                // 执行删除
                $res = DB::table('user')->where('id',$id)->delete();
                if($res){
                    echo 1;

                }else{
                    echo 0;
                }
            }


}
